//
//  HLYiYanViewController.m
//  Various
//
//  Created by HarwordLiu on 16/3/23.
//  Copyright © 2016年 Harword. All rights reserved.
//

#import "HLYiYanViewController.h"
#import "HLNetWorkTool.h"
#import "HLYiYanModel.h"
#import "HLYiYanCollectionViewCell.h"
#import "HLYiYanCollectionViewLayout.h"
#import <MJRefresh.h>
#import "HLYiYanContentViewController.h"



@interface HLYiYanViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, HLYiYanContentVCDelegate>

@property (nonatomic, assign) NSInteger pages;
@property (nonatomic, strong) NSMutableArray *arrModel;

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UIView *snapView;
@property (nonatomic, strong) NSIndexPath *indexPath;

@end

@implementation HLYiYanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatSubView];
}

- (void)viewWillAppear:(BOOL)animated {
//    [self dismissAnimation];
    [self.collectionView reloadData];
    
}
- (void)dismissAnimation {
    [self.tabBarController hiddenTabBar:NO];
    HLYiYanCollectionViewCell *cell = (HLYiYanCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:self.indexPath];
    CGRect tempFrame = [cell convertRect:cell.frame toView:nil];
    [self.view addSubview:self.snapView];
    [UIView animateWithDuration:0.5 animations:^{
        self.snapView.alpha = 1;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 animations:^{
            self.snapView.frame = tempFrame;
        } completion:^(BOOL finished) {
//            [self.snapView removeFromSuperview];
        }];
    }];
}


- (void)creatSubView {
    self.navigationItem.title = @"荐";
    
    self.arrModel = [NSMutableArray array];
    HLYiYanCollectionViewLayout *layout = [[HLYiYanCollectionViewLayout alloc] init];
    
    layout.itemSize = CGSizeMake(sWEIGHT * 314, sHEIGHT * 653 - 64 - 49);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, sWEIGHT * 414, sHEIGHT * 733 - 64 - 49) collectionViewLayout:layout];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[HLYiYanCollectionViewCell class] forCellWithReuseIdentifier:YIYANCOLLECTIONVIEWCELL];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh:)];
    [self.collectionView.mj_header beginRefreshing];
    
    self.collectionView.mj_footer = [MJRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh:)];
    


    
}
- (void)headerRefresh:(MJRefreshNormalHeader *)header {
    [self getDataSourceFromNetWith:0];
    

    
}

- (void)footerRefresh:(MJRefreshFooter *)footer {
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x < 0) {
        NSLog(@"--");
    }
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    if (scrollView.contentOffset.x < 0) {
        [self getDataSourceFromNetWith:0];
    } else if (targetContentOffset->x > (self.arrModel.count - 2) * 314 * sWEIGHT) {
        [self getDataSourceFromNetWith:0];
    }
}




#pragma mark - collectionView Data Source

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arrModel.count ? : 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HLYiYanCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:YIYANCOLLECTIONVIEWCELL forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.model = self.arrModel[indexPath.row];
    return cell;
    
    
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HLYiYanContentViewController *view = [[HLYiYanContentViewController alloc] init];
    view.arrModel = self.arrModel;
    view.indexPath = indexPath;
    view.cell = (HLYiYanCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    截图
    view.snapShotView = [view.cell snapshotViewAfterScreenUpdates:YES];
    view.navigationItem.hidesBackButton = YES;
    view.delegate = self;
    [self.navigationController pushViewController:view animated:NO];
}

#pragma mark - YiYanContent delegate

- (void)changeCellIndexPath:(NSIndexPath *)indexPath sentSnapShotView:(UIView *)snapView{
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    CGPoint pointTemp = CGPointMake(self.collectionView.contentOffset.x - 50 * sWEIGHT, self.collectionView.contentOffset.y);
    NSLog(@"%f, %f", pointTemp.x, pointTemp.y);
    self.collectionView.contentOffset = pointTemp;
    self.snapView = snapView;
    self.indexPath = indexPath;
    
}


- (void)getDataSourceFromNetWith:(NSInteger)pages {
    NSDictionary *dicBody = @{@"ltid" : [NSString stringWithFormat:@"%ld", pages],
                          @"v": @"2"};
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dicBody options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSDictionary *dicHeader = @{@"Cookie" : @"JSESSIONID=699ED679CB3E7D3D5E79CA1A7EAAACE7",
                                @"Content-Type" : @"application/x-www-form-urlencoded"};
    [HLNetWorkTool postWithURL:WEBYIYAN Body:jsonStr BodyType:BodyTypeJSONString HttpHeader:dicHeader ResponseType:ResponseTypeJSON Progress:^(NSProgress *progress) {
        
    } Success:^(id result) {
        NSArray *arrResult = [result valueForKey:@"textcardlist"];
        NSMutableArray *tempArr = [NSMutableArray arrayWithArray:[HLYiYanModel getDataFromDataSource:arrResult]];
        [self.arrModel addObjectsFromArray:tempArr];
        [self.collectionView reloadData];

    } Failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
